<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/17
 * Time: 15:43
 */

namespace app\zucai\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Db;


class Article extends Admin
{
    public function index()
    {
        // 获取排序
        $order = $this->getOrder();

        // 获取筛选
        $map = $this->getMap();

        if (!$order) $order='create_time desc';

        $article=Db::name('article')->where($map)->order($order)->paginate(50);
        return ZBuilder::make('table')
            ->setTableName('article')
            ->hideCheckbox()
            ->addFilter('create_time,update_time') // 添加筛选
            ->addOrder('update_time,create_time') // 添加排序
            ->addTimeFilter('update_time')
            ->addColumns([
                ['__INDEX__', '序号'],
                ['title', '文章标题','text.edit'],
                ['img', '封面图','picture'],
                ['jjabstract', '文章简介','textarea.edit'],
                ['create_time', '创建时间'],
                ['update_time', '更新时间'],
                ['status', '状态','switch'],
                ['right_button', '操作', 'btn'],
            ])
            ->addTopButtons(['add'])
            ->addRightButtons('edit')
            ->setRowList($article)
            ->fetch();
    }

    public function edit($id=0)
    {

        $ispost=$this->request->isPost();
        if ($ispost){
            $post=$this->request->post();

            if (!$post['img']) $this->error('封面图不能为空');
            if (!$post['title']) $this->error('文章标题不能为空');
            if (!$post['jjabstract']) $this->error('文章简介不能为空');
            if (!$post['content']) $this->error('文章内容不能为空');

            isset($post['status'])?$post['status']=1:$post['status']=0;

            $post['update_time']=date('Y-m-d H:i:s',time());

            if (!Db::name('article')->where('id',$id)->update($post)){
                $this->error('更新文章失败');
            }
            else{
                $this->success('更新文章成功','index');
            }
        }
        $article=Db::name('article')->where('id',$id)->find();

        return ZBuilder::make('form')
            ->addFormItems([
                ['switch','status','是否启用','',$article['status'],['shape'=>'square']],
                ['image','img','文章封面图','',$article['img']],
                ['text','title','文章标题','',$article['title']],
                ['text','jjabstract','文章简介','',$article['jjabstract']],
                ['summernote','content','文章内容','',$article['content']],
            ])
            ->fetch();
    }

    public function add()
    {
        $ispost=$this->request->isPost();
        if ($ispost){
            $post=$this->request->post();

            if (!$post['img']) $this->error('封面图不能为空');
            if (!$post['title']) $this->error('文章标题不能为空');
            if (!$post['jjabstract']) $this->error('文章简介不能为空');
            if (!$post['content']) $this->error('文章内容不能为空');

            isset($post['status'])?$post['status']=1:$post['status']=0;

            $post['update_time']=date('Y-m-d H:i:s',time());

            if (!Db::name('article')->insert($post)){
                $this->error('新增文章失败');
            }
            else{
                $this->success('新增文章成功','index');
            }
        }

        return ZBuilder::make('form')
            ->addFormItems([
                ['switch','status','是否启用','','',['shape'=>'square']],
                ['image','img','文章封面图'],
                ['text','title','文章标题'],
                ['text','jjabstract','文章简介'],
                ['summernote','content','文章内容'],
            ])
            ->fetch();
    }
}